package com.itheima.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.ssm.domain.Orders;
import com.itheima.ssm.service.IOrderService;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/orders")
@Secured("ROLE_ADMIN")
public class OrderController {

    @Autowired
    private IOrderService orderService;

//分页后   查询所有



    @RequestMapping("/findAll")//page  当前页    size 当前页显示的条数

    public ModelAndView find(@RequestParam(name = "page",required = true,defaultValue = "1")int page ,
                             @RequestParam(name = "size",required = true,defaultValue = "4")int size ) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        List<Orders> ordersList = orderService.findAll(page,size);
        //pageInfo 就是一个分页的javaBean
        PageInfo pageInfo = new PageInfo(ordersList);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("orders_page_list");
        return modelAndView;
    }

    //数据回显
   //@ResponseBody
    @RequestMapping("/toUpdate")
    public ModelAndView update(String id){
        Orders orders = orderService.selectById(id);
        System.out.println(orders);
        ModelAndView mv = new ModelAndView();
        mv.addObject("orders",orders);
        mv.setViewName("order-update");

        return  mv;
    }

//修改
/*@RequestMapping("/update")
public String update(Orders orders){
        orderService.update(orders);
        return "redirect:findAll";
}*/

//删除
    @RequestMapping("/delete")
    public String delete(String[] ids){
        for (String id : ids) {
            orderService.deleteById(id);
        }
        return "redirect:findAll";
    }

    ///查找一个
    @RequestMapping("/findById")
    public ModelAndView findById(String id){
        Orders orders = orderService.selectById(id);
        ModelAndView mv = new ModelAndView();
        System.out.println(orders);
        mv.addObject("orders", orders);
        mv.setViewName("order-show");
        return mv;
    }
}
